<?php
//decode by http://奥森世纪/
namespace app\home\controller;

use think\Db;
use think\Log;
use app\home\model\Follow;
use app\admin\model\Config;
class Vip extends Base{

    public function __construct(){
        parent::__construct();

        $this->member = $this->checkLogin();
    }

    public function index(){
        $list = Db::name("member_level")->where("is_effect = 1")->select();
        $info = Db::name("member")->where("uid = {$this->member->uid}")->find();
/*        $config = Config::getInfo();
        if(check_array($config['setting'])){
            $setting = $config['setting'];
        }*/
        return $this->fetch(__FUNCTION__,[
            'list' => $list,
            'info' => $info,
           // 'setting'=>$setting
        ]);
    }
    public function freevip()
    {
        $config = Config::getInfo();
        if(check_array($config['setting'])){
            $setting = $config['setting'];
        }
        $list['tasksetting'] = $setting['vipsendtask'];//系统设置完成任务条数
        $list['peoplesetting'] = $setting['vipsendpeople'];//系统设置推荐有效好友个数
        $vipsendday = $setting['vipsendday']?$setting['vipsendday']:0;//系统设置赠送会员天数
        //推荐有效好友个数
        $list['peoplecount'] = \db('member')
            ->alias('m')
            ->join("tb_withdraw n","n.uid=m.uid")
            ->field("n.id")
            ->where(['m.parent_uid'=>$this->member['uid'],'n.status'=>1])
            ->count('*');
        //完成任务数
        $list['taskcount'] = $this->member['over_task_num'];
        if(request()->isAjax()){
            $type = trim(params('type'));
            if($type){
                if($type == 1){
                    if($list['taskcount']<$list['tasksetting']){
                        message('任务条数没达到要求','','error');
                    }
                    if($this->member['viptaskcount'] == 1){
                        message('您已领取该条目','','error');
                    }
                    $res = \db('member')->where(['uid'=>$this->member['uid']])->data(['viptaskcount'=>1])->update();
                    if($res){
                        if($this->member['vippeoplecount']==1){
                            if($this->member['level'] == 1){
                                $data['level'] = 2;
                                $data['level_time'] = time()+$vipsendday*24*3600;
                            }else{
                                $data['level_time'] = $this->member['level_time']+$vipsendday*24*3600;
                            }
                            if(\db('member')->where(['uid'=>$this->member['uid']])->data($data)->update()){
                                message('领取成功!!!','reload','success');
                            }else{
                                message('系统繁忙:-2','','error');
                            }
                        }else{
                            message('领取成功!!!','reload','success');
                        }
                    }else{
                        message('系统繁忙:-3','','error');
                    }
                }
                if($type == 2){
                    if($list['peoplecount'] < $list['peoplesetting']){
                        message('有效会员数没达到要求','','error');
                    }
                    if($this->member['vippeoplecount'] == 1){
                        message('您已领取该条目','','error');
                    }
                    $res = \db('member')->where(['uid'=>$this->member['uid']])->data(['vippeoplecount'=>1])->update();
                    if($res){
                        if($this->member['viptaskcount']==1){
                            if($this->member['level'] == 1){
                                $data['level'] = 2;
                                $data['level_time'] = time()+$vipsendday*24*3600;
                            }else{
                                $data['level_time'] = $this->member['level_time']+$vipsendday*24*3600;
                            }
                            $data['toptime'] = $setting['zengtop'];
                            if(\db('member')->where(['uid'=>$this->member['uid']])->data($data)->update()){
                                message('领取成功!!!','reload','success');
                            }else{
                                message('系统繁忙:-4','','error');
                            }
                        }else{
                            message('领取成功!!!','reload','success');
                        }
                    }else{
                        message('系统繁忙:-5','','error');
                    }
                }
            }
            message('系统繁忙:-1','','error');
        }
        //任务完成百分比
        $pre_task=bcdiv($list['taskcount'],$list['tasksetting'],2)*100;
        if($pre_task>=100)
        {
            $list['taskpresent']=100;
        }
        else
        {
            $list['taskpresent']=$pre_task;
        }
        //游戏推荐完成百分比
       $pre_people=round($list['peoplecount']/$list['peoplesetting'],2)*100;
    
        if($pre_people>=100)
        {
            $list['peoplepersent']=100;
        }
        else
        {
            $list['peoplepersent']=$pre_people;
        }
        return $this->fetch(__FUNCTION__,[
            'list'=>$list,
            'member'=>$this->member
        ]);
    }

    public function openvip(){
        //  $this->giverepay($this->member->uid);
        // die;
        
        $params = request()->post();
        $id = trim(params('id'));
        $info = Db::name("member_level")->where("id = {$id}")->find();
        $memberres = Db::name("member")->where("uid = {$this->member->uid}")->find();

        
            if($info['have_money'] > $this->member->credit2){
                $ms['status']= 2;
                $ms['message'] = "余额不足，请充值";
                exit(json_encode($ms));
            }
            
            $data['credit2'] = $this->member->credit2 - $info['have_money'];
            $ying = $info['have_money'];
            
        $config = Config::getInfo();
        if(check_array($config['setting'])){
            $setting = $config['setting'];
        }
        
        $level_time=$this->member->level_time;
        
        if($level_time>0 && $level_time >=time()){
        	$data['level_time'] = $level_time+$info['term']*24*60*60;
        }else{
        	$data['level_time']=time()+$info['term']*24*60*60;
        }
        
           if($info['level']>$this->member->level){
        		$data['level']=$info['level'];
           }
        
        
        
        $data['toptime'] = $setting['zengtop'];
		
        $res = Db::name("member")->where("uid = {$this->member->uid}")->update($data);
        if($res){
            $dede['uid'] = $this->member->uid;
            $dede['type'] = 'credit2';
            $dede['num'] = '-'.$ying;
            $dede['title'] = '开通vip';
            $dede['remark']="您已开通{$info['title']}";
            $dede['create_time'] = time();
            $dede['balance']=$data['credit2'];
            $stas = Db::name("credit_record")->insert($dede);
            if($stas){
                $this->giverepay($this->member->uid,$id);
                $ms['status']= 1;
                $ms['message'] = "开通成功";
                exit(json_encode($ms));
            }else{
                $ms['status']= 0;
                $ms['message'] = "开通失败";
                exit(json_encode($ms));
            }

        }else{
            $ms['status']= 0;
            $ms['message'] = "开通失败";
            exit(json_encode($ms));
        }


    }

    function giverepay($uid,$level_id){
        $level_info = Db::name("member_level")->where("id = {$level_id}")->find();
        $member_info = Db::name("member")->where("uid = {$uid}")->find();

        ////////////////////////////////////////////////
        if($member_info['parent_uid'] > 0){
            $up_1 =  Db::name("member")->where("uid = {$member_info['parent_uid']}")->find();
            $data['credit2'] = $up_1['credit2'] + $level_info['repay_1'];
            $res = Db::name("member")->where("uid = {$up_1['uid']}")->update($data);
            if($res){
                $dede['uid'] = $up_1['uid'];
                $dede['type'] = 'credit2';
                $dede['num'] = '+'.$level_info['repay_1'];
                $dede['title'] = '返佣';
                $dede['remark']="{$uid}开通vip返佣";
                $dede['create_time'] = time();
                $stas = Db::name("credit_record")->insert($dede);
                unset($dede['type']);
                unset($dede['title']);
                $stasa = Db::name("invitation_rebate_record")->insert($dede);
            }
            ///////////////////////////////////////////////
            if($up_1['parent_uid'] > 0){
                $up_2 =  Db::name("member")->where("uid = {$up_1['parent_uid']}")->find();
                $data['credit2'] = $up_2['credit2'] + $level_info['repay_2'];
                $res2 = Db::name("member")->where("uid = {$up_2['uid']}")->update($data);
                if($res2){
                    $dede1['uid'] = $up_2['uid'];
                    $dede1['type'] = 'credit2';
                    $dede1['num'] = '+'.$level_info['repay_2'];
                    $dede1['title'] = '返佣';
                    $dede1['remark']="{$uid}开通vip返佣";
                    $dede1['create_time'] = time();
                    $stas1 = Db::name("credit_record")->insert($dede1);
                    unset($dede1['type']);
                    unset($dede1['title']);
                    $stasaa1 = Db::name("invitation_rebate_record")->insert($dede1);
                }
                ///////////////////////////////////////////////
                if($up_2['parent_uid'] > 0){
                    $up_3 =  Db::name("member")->where("uid = {$up_2['parent_uid']}")->find();
                    $data['credit2'] = $up_3['credit2'] + $level_info['repay_3'];
                    $res3 = Db::name("member")->where("uid = {$up_3['uid']}")->update($data);
                    if($res3){
                        $dede['uid'] = $up_3['uid'];
                        $dede['type'] = 'credit2';
                        $dede['num'] = '+'.$level_info['repay_3'];
                        $dede['title'] = '返佣';
                        $dede['remark']="{$uid}开通vip返佣";
                        $dede['create_time'] = time();
                        $stas3 = Db::name("credit_record")->insert($dede);
                        unset($dede['type']);
                        unset($dede['title']);
                        $stasab3 = Db::name("invitation_rebate_record")->insert($dede);
                    }
                }
            }

        }

    }



}